package com.example.service;


import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @author kangshuai
 */
/**
 * @author mcl
 * @date 2021/11/6 17:36
 * @description
 */
@Slf4j
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private RestHighLevelClient client;
    private static final String POSITION_INDEX = "lagou_log";

    @Override
    public List<Map<String, Object>> searchPos(String keyword,int pageNo, int pageSize) {

        if (pageNo <= 1){
            pageNo = 1;
        }

        ArrayList<Map<String, Object>> list = new ArrayList<>();

        // 搜索
        SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder positionNameQueryBuilder = QueryBuilders.matchQuery("positionName", keyword);
        // 分页设置
        searchSourceBuilder.from((pageNo-1)*pageSize);

        searchSourceBuilder.size(pageSize);
        searchSourceBuilder.query(positionNameQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        // 执行搜索
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = null;
        try {
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("IOException: ", e);
        }
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }

        pageSize = 5 - list.size();

        if (pageSize <= 0) {
            return list;
        }

        // 不够5条数据查询福利最好的补全
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchQueryBuilder positionadvantageQueryBuilder = QueryBuilders.matchQuery("positionAdvantage", "美女多 员工福利好");
        boolQueryBuilder.mustNot(positionNameQueryBuilder);
        boolQueryBuilder.must(positionadvantageQueryBuilder);

        searchSourceBuilder.size(pageSize);
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        // 执行搜索
        searchRequest.source(searchSourceBuilder);
        try {
            searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("IOException: ", e);
        }
        hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            list.add(hit.getSourceAsMap());
        }

        return list;
    }

}
